package integral;

import models.BaseModel;
import models.constants.DeletedStatus;
import org.apache.commons.lang.StringUtils;
import play.db.jpa.JPA;
import product.merchant.ProductSpecMerchant;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;

/**
 * 积分产品
 * Created by youliangcheng on 17/11/18.
 */
@Entity
@Table(name = "integral_products")
public class IntegralProduct  extends BaseModel{


    /**
     * 关联的积分活动
     */
    @ManyToOne
    @JoinColumn(name = "integral_ctivity_id")
    public IntegralActivity integralActivity;

    /**
     * 关联产品
     */
    @JoinColumn(name = "product_spec_merchant_id")
    @ManyToOne
    public ProductSpecMerchant productSpecMerchant;


    /**
     * 销售价格
     */
    @Column(name = "sale_price")
    public Double salePrice;

    /**
     * 积分兑换所需积分数量
     */
    @Column(name = "integral_price")
    public Double integralPrice;

    /**
     * 活动图片
     */
    @Column(name = "image_path")
    public String imagePath;


    /**
     * 逻辑删除状态
     */
    @Column(name = "deleted")
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;


    /**
     * 根据活动,查询活动下的积分兑换产品
     * @param integralActivityId
     * @return
     */
    public static List<IntegralProduct>  loadByActivityId(long integralActivityId){
        return IntegralProduct.find("integralActivity.id = ? and deleted = ?" , integralActivityId , DeletedStatus.UN_DELETED).fetch();
    }


    /**
     * 批量删除
     * @param deleteIds
     */
    public static void deletedByIds(Long[] deleteIds){
        String dels = UStringUtil.concatStr(",",deleteIds);
        String updateSql = " update integral_products a set a.deleted = 1 where a.id in("+dels+") ";

        JPA.em().createNativeQuery(updateSql).executeUpdate();
    }

    public static IntegralProduct findByIntegralProductId(long id){
        return IntegralProduct.find("deleted = ? and id = ?" , DeletedStatus.UN_DELETED , id).first();
    }




}
